<template>
  <div class="lb-dis-relation">
    <top-nav></top-nav>
    <div class="page-main">
      <el-tabs type="border-card">
        <el-tab-pane label="分销关系">
          <el-table
            v-loading="loading"
            :data="tableData"
            :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
            style="width: 100%"
          >
            <el-table-column prop="id" label="ID" width="60"> </el-table-column>
            <el-table-column prop="nickName" label="微信昵称">
            </el-table-column>
            <el-table-column label="微信头像">
              <template slot-scope="scope">
                <lb-image :src="scope.row.avatarUrl" />
              </template>
            </el-table-column>
            <el-table-column prop="create_money" label="产生收益(元)">
            </el-table-column>
            <el-table-column prop="total_profit" label="总收益(元)">
            </el-table-column>
            <el-table-column prop="total_postal" label="已提现(元)">
            </el-table-column>
            <el-table-column prop="pid" label="上线id"> </el-table-column>
            <el-table-column prop="p_nickName" label="上线昵称">
            </el-table-column>
            <el-table-column label="上线头像">
              <template slot-scope="scope">
                <lb-image :src="scope.row.p_avatarUrl" />
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间">
              <template slot-scope="scope">
                <p>{{ scope.row.create_time | handleTime(1) }}</p>
                <p>{{ scope.row.create_time | handleTime(2) }}</p>
              </template>
            </el-table-column>
          </el-table>
          <lb-page
            :batch="false"
            :page="page"
            :pageSize="limit"
            :total="total"
            @handleSizeChange="handleSizeChange"
            @handleCurrentChange="handleCurrentChange"
          >
          </lb-page>
        </el-tab-pane>
        <el-tab-pane label="查找关系">
          <el-form
            @submit.native.prevent
            :inline="true"
            :model="searchForm"
            class="search-form"
          >
            <el-form-item label="用户ID">
              <el-input
                v-model="searchForm.id"
                placeholder="请输入需要查找用户的ID"
                style="width: 200px"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchRelationList(1)">{{
                $t('action.search')
              }}</el-button>
            </el-form-item>
          </el-form>
          <el-collapse class="lb-collapse" v-model="activeNames">
            <el-collapse-item
              v-for="(item, index) in collapseData"
              :key="index"
              :title="item.title"
              :name="index.toString()"
            >
              <el-table :data="item.data" style="width: 100%">
                <el-table-column prop="id" label="用户ID"> </el-table-column>
                <el-table-column prop="nickName" label="微信昵称">
                </el-table-column>
                <el-table-column label="微信头像">
                  <template slot-scope="scope">
                    <lb-image :src="scope.row.avatarUrl" />
                  </template>
                </el-table-column>
                <el-table-column prop="date" label="加入时间">
                  <template slot-scope="scope">
                    <p>{{ scope.row.create_time | handleTime(1) }}</p>
                    <p>{{ scope.row.create_time | handleTime(2) }}</p>
                  </template>
                </el-table-column>
                <el-table-column prop="create_money" label="产生收益(元)">
                </el-table-column>
                <el-table-column prop="total_profit" label="总收益(元)">
                </el-table-column>
                <el-table-column prop="total_postal" label="已提现(元)">
                </el-table-column>
              </el-table>
            </el-collapse-item>
          </el-collapse>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
      searchForm: {
        id: ''
      },
      activeNames: ['0', '1', '2'],
      collapseData: [
        {
          title: '当前用户信息',
          data: []
        },
        {
          title: '上线信息',
          data: []
        },
        {
          title: '下线列表',
          data: []
        }
      ]
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let { page, limit } = this
      this.$api.getDistributionRelationList({ page, limit }).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.total = res.data.total
          this.tableData = res.data.data
        }
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    },
    searchRelationList (flag) {
      if (flag) this.searchForm.page = 1
      let { searchForm } = this
      if (!searchForm.id) {
        this.$message.error('请输入内容后再搜索')
      } else {
        this.$api.getDistributionRelationLookupList(searchForm).then(res => {
          console.log(res)
          if (res.code === 200) {
            let { down, self, up } = res.data
            this.collapseData = [
              {
                title: '当前用户信息',
                data: self
              },
              {
                title: '上线信息',
                data: up
              },
              {
                title: '下线列表',
                data: down
              }
            ]
          }
        })
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-dis-relation {
  width: 100%;
  .page-main {
    .el-table {
      img {
        width: 60px;
        height: 60px;
      }
    }
    .search-form {
      .el-input {
        width: 400px;
      }
    }
  }
}
</style>
